/******************************************************************** 
Prueba del modulo comparador
*********************************************************************/ 
//#define __MSP430F147__ //__MSP430F2274__
//#include "io430.h"

#include "io430x22x4.h"
#include "timer.h" 
#include "intrinsics.h"
#include "comparator.h"

//-------------------------- comienzo funciones auxiliares -----------------------------
void system_init();    // timerA, registrar la ISR
void system_sleep();   // modo LPM3
void comparator_init();

/*********************************************************************
Inicializacion del sistema.
Debe inicializar el timerA, registrar la ISR
*********************************************************************/ 

void system_init(){

/* Internal Very Low Power, Low Frequency Oscillator
The internal very-low-power, low-frequency oscillator (VLO) provides a typical frequency of 12 kHz 
VLOCLK source is selected by setting LFXT1Sx = 10 when XTS = 0. 
The OSCOFF bit disables the VLO for LPM4. 
The LFXT1 crystal oscillators are disabled when the VLO is selected reducing current consumption.
*/

    BCSCTL3 =  LFXT1S_2;
      
    P1DIR |= 0x01;    // P1.0 output, Led rojo conectado a P1.0, Led verde conectado a P1.1
    TACCTL0 = CCIE;  // modo comparador
    TACCR0 = 375;   // umbral=375 (250ms)
    TACTL = TASSEL_1  + MC_1 + TACLR + ID_3; // up_mode, ACLK, clear, /8
    __enable_interrupt(); // GIE=1
    return;
    }

/***********************************************************************
El sistema pasa a un modo inactivo de bajo consumo (LPM3)
**********************************************************************/ 
void system_sleep(){
  __bis_SR_register(LPM3_bits+GIE);             // Enter LPM3
    return;
    }

//-------------------------- fin funciones auxiliaes -----------------------------------------

  timer_t main_actual_time;

main() {

    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
 
    //Inicializacion del timer:
    timer_t init_time_value=(timer_t){0,0,0,0};
    timer_init (init_time_value);
    
    //Inicializacion del sistema:
     system_init();
     //system_sleep();
     comparator_init(test_callback);

    while(1){
      main_actual_time = timer_actual_time(); // copia cuenta de la hora a variable global
    }
}

#pragma vector=TIMERA0_VECTOR   
__interrupt void tic250 (void){ 
    timer_inc();
    P1OUT ^= 0x01;           // Toggle P1.0 (led)
    }
